package com.chatsocket.httpController;

import javax.servlet.http.HttpServletRequest;

import com.chatsocket.util.User;

public abstract class BaseController {
	protected final static String USER="_http_session_user";
	/**获取http请求根路径*/
	protected String getHttpBasePath(HttpServletRequest request){
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		return basePath;
	}
	/**获取ws请求根路径*/
	protected String getWSBasePath(HttpServletRequest request){
		String path = request.getContextPath();
		String basePath = "ws://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		return basePath;
	}
	/**从session中获取user*/
	protected User getUser(HttpServletRequest request){
		Object obj = request.getSession().getAttribute(USER);
		if(obj==null){
			return null;
		}else{
			return  (User)obj;
		}
	}
	/**从sesssion中获取userId*/
	protected Long getUserId(HttpServletRequest request){
		User user = getUser(request);
		if(user==null){
			return null;
		}else{
			return user.getId();
		}
	}
}
